import 'package:get/get.dart';
import 'package:kkhousekeep/net/home_visitor_connect.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/page/home/convenient/visitor/record_add.dart';
import 'package:kkhousekeep/page/home/convenient/visitor/record_detail.dart';

import '../../../../models/home_visitor_record_entity.dart';

class VisitorRecordPageController extends GetxController {
  var visitorRecordList = <HomeVisitorRecordObjList>[].obs;
  late HomeVisitorConnect connect;

  @override
  void onInit() {
    super.onInit();
    connect = Get.put(HomeVisitorConnect());
    _initList();
  }

  _initList() async {
    var response = await connect.getRecordList();
    if (response == null) {
      return;
    }
    if (response.obj.list.isEmpty) {
      return;
    }
    if (response.code == ResponseCode.success) {
      visitorRecordList.value = response.obj.list;
    }
  }

  void buildVisitorPassPage() {
    Get.to(() => const BuildVisitorPassPage())?.whenComplete(() => _initList());
  }

  void buildVisitorDetail(int index) {
    Get.to(
      () => VisitorRecordDetail(
        visitorId: visitorRecordList[index].id,
      ),
    );
  }
}
